﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using System.Drawing;
using Bamboo.Core.Common.Reflection;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using TinyERP.Framework.Controls;
using System.Globalization;
using System.Threading;
using TinyERP.Framework.Config;
using TinyERP.Core.DTO;
using TinyERP.Framework;
using TinyERP.Core.Business;
using TinyERP.Framework.Controls.Docking;


namespace TinyERP.Core.View.Controls
{
    public class UIManager
    {
        private static UIManager instance;
        private KApplicationRibbon applicationRibbon;
        private IContentPane contentPanel;
        public IUINodeService UINodeService
        {
            get
            {
                return (IUINodeService)ServiceLocator.GetService("UINodeServiceProxy");
            }
        }
        public KApplicationRibbon ApplicationRibbon
        {
            get
            {
                return applicationRibbon;
            }
            set
            {
                applicationRibbon = value;
                this.applicationRibbon.OnSelectedNode += new SelectNodeHandler(OnRibbonSelectNode);
            }
        }

       
       
       
        public IContentPane ContentPanel
        {
            get
            {
                return contentPanel;
            }
            set
            {
                contentPanel = value;
                this.contentPanel.OnSelectedPane += new ContentPaneSelectedHandler(OnSelectedPane);
            }
        }

        private void OnSelectedPane(object sender, UINodeDTO formNode)
        {
            ApplicationRibbon.SelectNode(formNode);
        }
        private void OnRibbonSelectNode(object sender, UINodeDTO selectedNode)
        {
            ContentPanel.ShowPane(selectedNode);
        }
        public void LoadGlobalShortcuts()
        {
            StandardShortcutKey.CLOSE = ApplicationConfiguration.GetInstance().CloseShortcut;
            StandardShortcutKey.DELETE = ApplicationConfiguration.GetInstance().DeleteShortcut;
            StandardShortcutKey.FIND = ApplicationConfiguration.GetInstance().FindShortcut;
            StandardShortcutKey.EDIT = ApplicationConfiguration.GetInstance().EditShortcut;
            StandardShortcutKey.NEW = ApplicationConfiguration.GetInstance().NewShortcut;
        }

        private UIManager()
        {

        }
        public string GetTitleOfUINode(UINodeDTO uiNode)
        {
            string titleText = ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey(uiNode.ResourceKey);
            if (string.IsNullOrEmpty(titleText))
            {
                return uiNode.Title;
            }
            return titleText;
        }
        public static UIManager GetInstance()
        {
            if (instance == null)
            {
                instance = new UIManager();
            }
            return instance;
        }


        public void ShowPane(UINodeDTO form)
        {
            if (!string.IsNullOrEmpty(form.UIClassName))
            {
                contentPanel.ShowPane(form);
            }
        }
        private void OnNavigatorSelectedNode(object sender, UINodeDTO selectedNode)
        {
            
        }
        public void ReloadNavigator()
        {
            Session.ApplicationNodes = UINodeService.GetVisibleUINodeTreeOfUser(Session.LoginUser);
            this.applicationRibbon.BuildNavigator();
        }
       
        
        public void ChangeLanguage(string language, Form form)
        {
            CultureInfo cultureInfo = new CultureInfo(language);
            
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
            ApplyCurrentCultureInfo(form);
            ReloadNavigator();
        }
        public void ApplyCurrentCultureInfo(Form form)
        {
            
            foreach (Control control in form.Controls)
            {
                ChangeLanguageOfControl(control);
            }
        }


        private void ChangeLanguageOfControl(Control control)
        {
            if (control is ILocalizable)
            {
                ((ILocalizable)control).UpdateLanguage();
            }
            if (control.Controls != null && control.Controls.Count > 0)
            {
                foreach (Control childControl in control.Controls)
                {
                    ChangeLanguageOfControl(childControl);
                }
            }
        }
        public static Form MainScreen { get; set; }
    }
}
